package com.product.android.commonInterface.contact;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import com.common.android.utils.JSONUtils;
import com.common.android.utils.StringUtils;
import com.product.android.utils.TimeUtils;

public class OapUser implements Serializable {

	private static final long serialVersionUID = 1L;
	// private static final String TAG = "OapUser";
	private long uid;
	private long uap_uid;
	private long fid;
	private int unitid;
	private int gender;
	private int type;
	private int isactive = 0; // 用户默认激活
	private int sysavatar;
	private int usercount;
	private String studentid;
	private String username;
	private String nickname;
	private String signature;
	private String joindate;
	private String telephone;
	private String mobilephone;
	private String email;
	private String updatetime;
	private String depts;
	private String unitName;
	private String note;
	private String address;
	private String fax;
	private String birthday;
	private String blood;
	private String site;
	private String description;
	private String postcode;
	private String workid;
	private String duty;
	private String spell1 = "";
	private String spell2 = "";
	private String course_name;
	private int courseid;
	private List<OapUser> guardians;
	private String imageUrl = null;
	private int contact_open;// 默认=0，是否公开联系方式（0为不公开，1为向好友公开，2向所有人公开）
	private String province;// 省份（16位字符）
	private String city;// 城市（16位字符）
	private int userver;// 用户资料修改版本号
	private int classid;// 学生用户所在班级id
	private String seniorSchool;// 所在高中名称
	private boolean hasGuide; // true表示已引导，false表示尚未引导

	// 保密
	public static final int SECRET = 0;

	// 男
	public static final int MALE = 1;

	// 女
	public static final int FEMALE = 2;

	// 0为不公开，1为向好友公开，2向所有人公开
	public static final int OPEN_TO_NOONE = 0;
	public static final int OPEN_TO_FRIENDS = 1;
	public static final int OPEN_TO_PUBLIC = 2;

	public void initValue() {
		username = "";
		nickname = "";
		signature = "";
		joindate = "";
		telephone = "";
		mobilephone = "";
		email = "";
		updatetime = "";
		depts = "";
		unitName = "";
		note = "";
		address = "";
		fax = "";
		birthday = "";
		blood = "";
		site = "";
		description = "";
		postcode = "";
		workid = "";
		duty = "";
		studentid = "";
		course_name = "";
		contact_open = OPEN_TO_NOONE;
		province = "";
		city = "";
	}

	public OapUser() {
		initValue();
	}

	public OapUser(JSONObject json) {
		initValue();
		initValueByUnit(json);
		// Log.d(TAG,String.format("[用户部门]=%s,[用户id]=%s",JSONUtils.getString(json,
		// "depts"),JSONUtils.getLong(json, "uid")));
	}

	// public String getImageUrl(){
	// if(imageUrl==null||"".equals(imageUrl)){
	// imageUrl = OapImageSupportCom.getFaceurl(fid);
	// imageUrl = HeadImageLoader.
	// }
	// return imageUrl;
	// }

	public String getCourse_name() {
		return course_name;
	}

	public void setCourse_name(String courseName) {
		course_name = courseName;
	}

	/**
	 * 学生获取家长列表
	 * 
	 * @param json
	 */
	public void initValueByStudent(JSONObject json) {
		JSONArray jsonarray = JSONUtils.getJSONArray(json, "guardians");
		OapUser user;
		JSONObject jsontemp;
		if (jsonarray != null) {
			if (guardians == null)
				guardians = new ArrayList<OapUser>();
			for (int i = 0; i < jsonarray.length(); i++) {
				jsontemp = JSONUtils.getJSONObject(jsonarray, i);
				if (jsontemp != null) {
					user = new OapUser();
					user.initBaseInfo(jsontemp);
					user.setType(3);
					if (guardians == null) {
						guardians = new ArrayList<OapUser>();
					}
					guardians.add(user);
				}
			}
		}
	}

	public void initValueByUnit(JSONObject json) {
		// Log.v(TAG,"initValueByUnit: "+json.toString());
		this.fid = JSONUtils.getLong(json, "uid");
		this.uap_uid = JSONUtils.getLong(json, "uap_uid");
		this.username = JSONUtils.getString(json, "username");
		this.nickname = JSONUtils.getString(json, "nickname");
		this.signature = JSONUtils.getString(json, "signature");
		this.joindate = JSONUtils.getString(json, "joindate");
		this.gender = JSONUtils.getInt(json, "gender");
		this.telephone = JSONUtils.getString(json, "telephone");
		this.mobilephone = JSONUtils.getString(json, "mobilephone");
		this.email = JSONUtils.getString(json, "email");
		this.updatetime = JSONUtils.getString(json, "updatetime");
		this.workid = JSONUtils.getString(json, "workid");
		this.depts = JSONUtils.getString(json, "depts");
		this.userver = JSONUtils.getInt(json, "userver");
		this.classid = JSONUtils.getInt(json, "class_id");
		// 服务端返回的格式"depts":["47507"] ，过滤双引号 depts = [47507]
		if (!TextUtils.isEmpty(depts)) {
			depts = depts.replace("\"", "");
		}
		this.unitName = JSONUtils.getString(json, "unitname");
		this.sysavatar = JSONUtils.getInt(json, "sysavatar");
		this.studentid = JSONUtils.getString(json, "studentid");
		this.spell1 = JSONUtils.getString(json, "spell1");
		this.spell2 = JSONUtils.getString(json, "spell2");
		this.birthday = JSONUtils.getString(json, "birthday");
		this.unitid = JSONUtils.getInt(json, "unitid");
		this.contact_open = JSONUtils.getInt(json, "contact_open");
	}

	public void initValueByFriend(JSONObject json) {
		// Log.v(TAG,"initValueByFriend: "+json.toString());
		this.fid = JSONUtils.getLong(json, "uid");
		this.uap_uid = JSONUtils.getLong(json, "uap_uid");
		this.username = JSONUtils.getString(json, "username");
		this.nickname = JSONUtils.getString(json, "nickname");
		this.signature = JSONUtils.getString(json, "signature");
		this.note = JSONUtils.getString(json, "note");
		this.updatetime = JSONUtils.getString(json, "updatetime");
		this.sysavatar = JSONUtils.getInt(json, "sysavatar");
		this.isactive = JSONUtils.getInt(json, "isactive");
		this.spell1 = JSONUtils.getString(json, "spell1");
		this.spell2 = JSONUtils.getString(json, "spell2");
		this.birthday = JSONUtils.getString(json, "birthday");
	}

	public void initBaseInfo(JSONObject json) {
		this.fid = JSONUtils.getLong(json, "uid");
		this.uap_uid = JSONUtils.getLong(json, "uap_uid");
		this.username = JSONUtils.getString(json, "username");
		this.nickname = JSONUtils.getString(json, "nickname");
		this.workid = JSONUtils.getString(json, "workid");
		this.type = JSONUtils.getInt(json, "type");
		this.joindate = JSONUtils.getString(json, "joindate");
		this.duty = JSONUtils.getString(json, "duty");
		this.gender = JSONUtils.getInt(json, "gender");
		this.telephone = JSONUtils.getString(json, "telephone");
		this.mobilephone = JSONUtils.getString(json, "mobilephone");
		this.email = JSONUtils.getString(json, "email");
		this.signature = JSONUtils.getString(json, "signature");
		this.updatetime = JSONUtils.getString(json, "updatetime");
		this.isactive = JSONUtils.getInt(json, "isactive");
		this.sysavatar = JSONUtils.getInt(json, "sysavatar");
		this.unitid = JSONUtils.getInt(json, "unitid");
		this.unitName = JSONUtils.getString(json, "unitname");
		this.studentid = JSONUtils.getString(json, "studentid");
		this.spell1 = JSONUtils.getString(json, "spell1");
		this.spell2 = JSONUtils.getString(json, "spell2");
		this.birthday = JSONUtils.getString(json, "birthday");
		this.contact_open = JSONUtils.getInt(json, "contact_open");
	}

	public void initExtInfo(JSONObject json) {
		this.gender = JSONUtils.getInt(json, "gender");
		this.fax = JSONUtils.getString(json, "fax");
		this.birthday = JSONUtils.getString(json, "birthday");
		this.blood = JSONUtils.getString(json, "blood");
		this.site = JSONUtils.getString(json, "site");
		this.description = JSONUtils.getString(json, "description");
		this.postcode = JSONUtils.getString(json, "postcode");
		this.address = JSONUtils.getString(json, "addr");
		this.province = JSONUtils.getString(json, "province");
		this.city = JSONUtils.getString(json, "city");
		JSONArray jsonarray = JSONUtils.getJSONArray(json, "emails");
		if (jsonarray != null && jsonarray.length() > 0) {
		}
	}

	public int getUserver() {
		return userver;
	}

	public void setUserver(int userver) {
		this.userver = userver;
	}

	public int getClassid() {
		return classid;
	}

	public void setClassid(int classid) {
		this.classid = classid;
	}

	public String getUnitName() {
		if (unitName == null || "null".equals(unitName) || "".equals(unitName)) {
			return "";
		}
		return unitName;
	}

	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	public long getFid() {
		return fid;
	}

	public void setFid(long fid) {
		this.fid = fid;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public long getUap_uid() {
		return uap_uid;
	}

	public void setUap_uid(long uapUid) {
		uap_uid = uapUid;
	}

	public long getNDUap_uid() { // 网龙版返回 Oap_uid
		return fid;
	}

	public String getUserName() {
		if (username == null || "null".equals(username) || "".equals(username)) {
			return "";
		}
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNickname() {
		if (nickname == null || "null".equals(nickname) || "".equals(nickname)) {
			return "";
		}
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getSignature() {

		return StringUtils.getFliteStr(signature);
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getJoindate() {
		if (joindate == null || "null".equals(joindate) || "".equals(joindate)) {
			return "";
		}
		return joindate;
	}

	public void setJoindate(String joindate) {
		this.joindate = joindate;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

	public void setContactOpen(int contact_open) {
		this.contact_open = contact_open;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCity() {
		if (city == null || "null".equals(city) || "".equals(city)) {
			return "";
		}
		return city;
	}

	public String getProvince() {
		if (province == null || "null".equals(province) || "".equals(province)) {
			return "";
		}
		return province;
	}

	public int getContactOpen() {
		return contact_open;
	}

	public String getTelephone() {
		if (telephone == null || "null".equals(telephone) || "".equals(telephone)) {
			return "";
		}
		return telephone;
	}

	public void setTelephone(String telephone) {
			this.telephone = telephone;
	}

	public String getMobilehone() {
		if (mobilephone == null || "null".equals(mobilephone) || "".equals(mobilephone)) {
			return "";
		}
		return mobilephone;
	}

	public List<OapUser> getGuardians() {
		return guardians;
	}

	public void setGuardians(List<OapUser> guardians) {
		this.guardians = guardians;
	}

	public String getSpell1() {
		return spell1;
	}

	public String getSpell2() {
		return spell2;
	}

	public String getStudentid() {
		return studentid;
	}

	public void setStudentid(String studentid) {
		this.studentid = studentid;
	}

	public void setSpell2(String spell2) {
		this.spell2 = spell2;
	}

	public void setSpell1(String spell1) {
		this.spell1 = spell1;
	}

	public void setMobilehone(String mobilehone) {
		this.mobilephone = mobilehone;
	}

	public int getUnitid() {
		return unitid;
	}

	public void setUnitid(int unitid) {
		this.unitid = unitid;
	}

	public String getEmail() {
		if (email == null || "null".equals(email) || "".equals(email)) {
			return "";
		}
		return email;
	}

	public int getUsercount() {
		return usercount;
	}

	public void setUsercount(int usercount) {
		this.usercount = usercount;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	/**
	 * 校友性别默认为女
	 * 
	 * @return
	 */
	public String getSex() {
		switch (gender) {
		case SECRET:
			return "女";
		case MALE:
			return "男";
		case FEMALE:
			return "女";
		}
		return "女";
	}

	public int getIsactive() { // 网龙版默认激活
		return isactive;
	}

	public void setIsactive(int isactive) {
		this.isactive = isactive;
	}

	public int getSysavatar() {
		return sysavatar;
	}

	public void setSysavatar(int sysavatar) {
		this.sysavatar = sysavatar;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUpdatetime() {
		return updatetime;
	}

	public void setUpdatetime(String updatetime) {
		this.updatetime = updatetime;
	}

	public String getAddress() {
		if (address == null || "null".equals(address) || "".equals(address)) {
			return "";
		}
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getBirthday() {
		if (birthday == null || "null".equals(birthday) || "".equals(birthday)) {
			return "";
		}
		if (birthday.equals("0000-00-00")) {
			return "-";
		}
		return birthday;
	}

	public Date getBirthdayToDate() {
		if (birthday == null || "".equals(birthday))
			return null;
		Pattern pattern = Pattern
				.compile("//^[\\d]{4}[-\\ ][\\d]{1,2}[-\\ ][\\d]{1,2}$//");
		if (pattern.matcher(birthday).matches()) {
			return null;
		}
		try {
			return new Date(birthday.replace("-", "/"));
		} catch (RuntimeException e) {
			return null;
		} catch (Error e) {
			return null;
		}
	}

	public String getAge() {
		return TimeUtils.getAgeByBirthday(birthday);
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getBlood() {
		if (blood == null || "null".equals(blood) || "".equals(blood)) {
			return "";
		}
		return blood;
	}

	public void setBlood(String blood) {
		this.blood = blood;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public String getDescription() {
		if (description == null || "null".equals(description) || "".equals(description)) {
			return "";
		}
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getWorkid() {
		return workid;
	}

	public void setWorkid(String workid) {
		this.workid = workid;
	}

	public String getDuty() {
		return duty;
	}

	public void setDuty(String duty) {
		this.duty = duty;
	}

	public String getNote() {
		return note;
	}

	public List<OapUnitRelation> getOapUnitRelations() {
		List<OapUnitRelation> OapUnitRelationList = new ArrayList<OapUnitRelation>();
		if (depts == null || "".equals(depts)) {
			OapUnitRelation oapUnitRelation = new OapUnitRelation();
			oapUnitRelation.setUid(getUid());
			oapUnitRelation.setFid(fid);
			oapUnitRelation.setUnitid(unitid);
			oapUnitRelation.setDeptid(0);
			oapUnitRelation.setUpdatetime(updatetime);
			OapUnitRelationList.add(oapUnitRelation);
		} else {
			String strarray[] = depts.split(",");
			if (strarray != null) {
				for (String strtemp : strarray) {
					OapUnitRelation oapUnitRelation = new OapUnitRelation();
					oapUnitRelation.setUid(getUid());
					oapUnitRelation.setFid(fid);
					oapUnitRelation.setUnitid(unitid);
					oapUnitRelation.setUpdatetime(updatetime);
					String deptidStr = "";
					if (strtemp.length() > 1) {// 避免越界
						try {
							deptidStr = strtemp.substring(1,
									strtemp.length() - 1);
							if (StringUtils.isNumeric(deptidStr)) {
								oapUnitRelation.setDeptid(Integer
										.valueOf(deptidStr));
							} else {
								oapUnitRelation.setDeptid(0);
							}
							OapUnitRelationList.add(oapUnitRelation);
						} catch (RuntimeException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return OapUnitRelationList;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public String getDepts() {
		if (depts == null || "null".equals(depts) || "".equals(depts)) {
			return "";
		}
		return depts;
	}

	public void setDepts(String depts) {
		this.depts = depts;
	}

	/**
	 * 去掉备注，默认返回用户名
	 * 
	 * @return
	 */
	public String getComment() {
		if (username != null && !"".equals(username))
			return username;
		else
			return fid + "";
		// if(note==null||note.equals("")||note.equals("null")){
		// return username;
		// }
		// else return note;
	}

	public String getPhone() {
		if (telephone != null && !telephone.equals("") && !"null".equals(telephone)) {
			return telephone;
		} else if(mobilephone != null && !mobilephone.equals("") && !"null".equals(mobilephone)){
			return mobilephone;
		}
			return "";
	}

	public String toString() {
		StringBuffer strbuf = new StringBuffer();
		strbuf.append("[oapuser]");
		strbuf.append(",uid=" + uid);
		strbuf.append(",username=" + username);
		return strbuf.toString();
	}

	/**
	 * @param courseid
	 *            the courseid to set
	 */
	public void setCourseid(int courseid) {
		this.courseid = courseid;
	}

	/**
	 * @return the courseid
	 */
	public int getCourseid() {
		return courseid;
	}

	/**
	 * 获取高校名称
	 * 
	 * <br>
	 * Created 2014-6-13 上午9:42:45
	 * 
	 * @return String
	 * @author huangyx
	 */
	public String getSeniorSchool() {
		if (seniorSchool == null || "null".equals(seniorSchool) || "".equals(seniorSchool)) {
			return "";
		}
		return seniorSchool;
	}

	/**
	 * 设置高校名称
	 * 
	 * <br>
	 * Created 2014-6-13 上午9:42:48
	 * 
	 * @param seniorSchool
	 *            seniorSchool
	 * @author huangyx
	 */
	public void setSeniorSchool(String seniorSchool) {
		this.seniorSchool = seniorSchool;
	}

	/**
	 * 设置引导标识
	 * 
	 * @param hasGuide
	 *            true表示已引导，false表示未引导
	 */
	public void setGuideFlag(boolean hasGuide) {
		this.hasGuide = hasGuide;
	}

	/**
	 * 获取引导标识位
	 * 
	 * @return 引导标识位
	 */
	public boolean getGuideFlag() {
		return hasGuide;
	}
}